using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;

using PlugNT.Safe;
using PlugNT.Common;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class SysModel
    {

        internal static string currTableName = WebConfig.TablePrefix + "sys_model";

        internal static Dictionary<int, SysModelInfo> models = null;
        private static List<int> indexs = null;


        static SysModel()
        {
            models = new Dictionary<int, SysModelInfo>();
            indexs = new List<int>();
            
            IDataReader dr = GetReader(-1, "*", "");
            
            if (dr != null)
            {
                while(dr.Read())
                {
                    int id = int.Parse(dr["id"].ToString());

                    SysModelInfo model = (dr["ass_name"].ToString().Trim() == "" ? new SysModelInfo() : (SysModelInfo)Utils.GetAssemblyInstance(dr["ass_name"].ToString().Trim(), dr["ass_class"].ToString().Trim())); 
                    
                    model.id = id;
                    model.ass_name = dr["ass_name"].ToString().Trim();
                    model.ass_class = dr["ass_class"].ToString().Trim();
                    model.name = dr["name"].ToString();
                    model.enable = dr["enable"].ToString() == "1";
                    model.is_fixed = dr["is_fixed"].ToString() == "1";
                    model.channel_no_list = dr["channel_no_list"].ToString();

                    model.msg_locked = dr["msg_locked"].ToString() == "1";
                    model.image_locked = dr["image_locked"].ToString() == "1";
                    model.msg_style = int.Parse(dr["msg_style"].ToString());
                    model.is_create_static = dr["is_create_static"].ToString() == "1";
                    model.is_ziphtml = dr["is_ziphtml"].ToString() == "1";
                    model.web_index = dr["web_index"].ToString();
                    model.dyn_article_path = dr["dyn_article_path"].ToString();
                    model.arr_str = dr["arr_str"].ToString();
                    model.ArrBind();

                    models.Add(model.id, model);
                    indexs.Add(model.id);
                }
                dr.Close();
            }
        }




        public static int GetDictionaryCount()
        {
            return indexs.Count;
        }

        public static SysModelInfo GetSysModelInfoByIndex(int index)
        {
            SysModelInfo info = null;
            models.TryGetValue(indexs[index], out info);
            return info;
        }

        public static bool Exists(int id)
        {
            return models.ContainsKey(id);
        }

        public static SysModelInfo GetSysModelInfoFromDictionary(int id)
        {

            SysModelInfo info=null;
            models.TryGetValue(id, out info);
            return info;
        }











        public bool Add(SysModelInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("[id],ass_name,ass_class,[name],[enable],is_fixed,channel_no_list,msg_locked,image_locked,msg_style,is_create_static,is_ziphtml,web_index,dyn_article_path,arr_str");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(model.id.ToString() + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.ass_name) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.ass_class) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.name) + "',");
            strSql.Append((model.enable ? "1" : "0") + ",");
            strSql.Append((model.is_fixed ? "1" : "0") + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.channel_no_list) + "',");

            strSql.Append((model.msg_locked ? "1" : "0") + ",");
            strSql.Append((model.image_locked ? "1" : "0") + ",");
            strSql.Append( model.msg_style.ToString() + ",");
            strSql.Append((model.is_create_static ? "1" : "0") + ",");
            strSql.Append((model.is_ziphtml ? "1" : "0") + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.web_index) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.dyn_article_path) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.arr_str) + "'");

            strSql.Append(")");

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                models.Add(model.id, model);
                indexs.Add(model.id);
                return true;
            }
            else
            {
                return false;
            }

        }






        public bool Update(SysModelInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[ass_name]='" + PlugNT.Safe.StringHelper.SqlFilter(model.ass_name) + "',");
            strSql.Append("[ass_class]='" + PlugNT.Safe.StringHelper.SqlFilter(model.ass_class) + "',");
            strSql.Append("[name]='" + PlugNT.Safe.StringHelper.SqlFilter(model.name) + "',");
            strSql.Append("[enable]=" + (model.enable ? "1" : "0") + ",");

            strSql.Append("channel_no_list='" + PlugNT.Safe.StringHelper.SqlFilter(model.channel_no_list) + "',");
            strSql.Append("[msg_locked]=" + (model.msg_locked ? "1" : "0") + ",");
            strSql.Append("[image_locked]=" + (model.image_locked ? "1" : "0") + ",");
            strSql.Append("[msg_style]=" + model.msg_style.ToString() + ",");
            strSql.Append("[is_create_static]=" + (model.is_create_static ? "1" : "0") + ",");
            strSql.Append("[is_ziphtml]=" + (model.is_ziphtml ? "1" : "0") + ",");
            strSql.Append("dyn_article_path='" + PlugNT.Safe.StringHelper.SqlFilter(model.dyn_article_path) + "',");
            strSql.Append("web_index='" + PlugNT.Safe.StringHelper.SqlFilter(model.web_index) + "'");
           

            strSql.Append(" where [id]=" + model.id.ToString());

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                models[model.id]= model;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateArr(int id, string strArr)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[arr_str]='" + PlugNT.Safe.StringHelper.SqlFilter(strArr) + "'");
            strSql.Append(" where [id]=" + id.ToString());

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateMenuJosn(int id, string menuJson)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[menu_json]='" + PlugNT.Safe.StringHelper.SqlFilter(menuJson) + "'");
            strSql.Append(" where [id]=" + id.ToString());

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateArticleJscode(int id, string jsCode)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[article_jscode]='" + PlugNT.Safe.StringHelper.SqlFilter(jsCode) + "'");
            strSql.Append(" where [id]=" + id.ToString());

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }






        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString());

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                models.Remove(id);
                indexs.Remove(id);
                return true;
            }
            else
            {
                return false;
            }
        }













        private static IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }


        public static object GetSingle(string colName, string andWhere)
        {
            return DbHelper.GetSingle("select top 1 [" + colName + "] from " + currTableName + " where 1=1 " + andWhere);
        }







        public static DataTable GetList(string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.*,b.title from  [" + currTableName + "] as a left join " + Channel.currTableName + " as b on a.channel_no_list=b.no_list where 1=1 ");
            strSql.Append((" " + andWhere).Replace(" and ", " and a."));
            return DbHelper.GetDataTable(strSql.ToString());
        }









    }
}

